﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HCRS.Implementation;
using HCRS;
using Common;

namespace EZCRS.HCMS.DataAcl
{
    /// <summary>
    ///  常客用户权限控制 的摘要说明
    /// </summary>
    public class FGBRoleSystems
    {
        public FGBRoleSystems()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        #region 语言版本
        /// <summary>
        /// 语言版本
        /// </summary>
        private static string _LanCode = "cn";
        #endregion

        #region 用户权限Cookie键值


        /// <summary>
        /// 用户权限Cookie键值
        /// </summary>
        private static string CookieKey = "FGBuser";
        #endregion

        #region 用户信息Cookie键值
        /// <summary>
        /// 用户信息Cookie键值
        /// </summary>
        private static string InfoKey = "FGBInfo";

        #endregion

        #region 语言版本代码
        /// <summary>
        /// 语言版本代码
        /// </summary>
        public static string LanCode
        {
            get { return _LanCode; }
            set { if (_LanCode != value) { _LanCode = value; } }
        }
        #endregion
      
        /// <summary>
       /// 获取散客网站预订引擎
       /// </summary>
        /// <param name="fit">散客网站预订引擎</param>
        /// <returns>散客网站预订引擎</returns>
        public static FGBGroup GetFGBEngine(FGBGroup fit)
        {
      

           if (fit.gid==0&&fit.hid==0)
           {
               return null;
           }
           else
           {
               string SQL = "";
               if (fit.id != 0)
               {
                    SQL = "select  * from hfg_org_domain where [id]=" + fit.id;
               }
               else if (fit.hid != 0)
               {
                   SQL = "select  * from hfg_org_domain where orgtype=1  and orgid=" + fit.hid;
               }
               else if (fit.gid != 0)
               {
                   SQL = "select  * from hfg_org_domain where orgtype=0  and orgid=" + fit.gid;
               }
               else
               {
                   return null;
               }
               DataTable dt = CommonDataAccess.GetTable(SQL,"gid");
               if (dt == null || dt.Rows.Count != 1)
               {
                  // return null;
                   SQL=" select  * from hfg_org_domain where orgtype=1  and orgid=" + fit.hid+" union all  select  * from hfg_org_domain where orgtype=0  and orgid=" + fit.gid;
                   dt = CommonDataAccess.GetTable(SQL, "gid");
                   if (dt == null || dt.Rows.Count < 1)
                   {
                       return null;
                   }
               }
               DataRow dr = dt.Rows[0];
               fit.id = (int)dr["id"];
               if ((int)dr["orgtype"] == 0)
               {
                   fit.gid = (int)dr["orgid"];
                   fit.hid = 0;
               }
               else
               {
                   fit.hid=(int)dr["orgid"];
                   fit.gid = 0;
               }              
               fit.webdomain = dr["webdomain"].ToString();
               fit.noscroll =(int) dr["noscroll"];
               fit.RateCode = dr["ratecode"].ToString();
               fit.css = dr["css"].ToString();
               fit.sloganresv = dr["sloganresv"].ToString();
               fit.sloganmod = dr["sloganmod"].ToString();
               fit.slogancxl = dr["slogancxl"].ToString();
               fit.slogannewresv = dr["slogannewresv"].ToString();
               fit.emailfrom = dr["emailfrom"].ToString();
               fit.emailalias = dr["emailalias"].ToString();
               fit.emailtitle = dr["emailtitle"].ToString();
               fit.languagetype =(int)dr["languagetype"];
               return fit;
           }
       }
     


        #region 获取当前用户ID
        /// <summary>
        /// 获取当前用户ID
        /// </summary>
        public static int UserID
        {
            get
            {
                try
                {
                    string uid = GetCookie("uid");
                    if (uid == "")
                    {
                        return -1;
                    }
                    else
                    {
                        return int.Parse(uid);
                    }
                }
                catch (Exception e)
                {
                    HCRS.Implementation.Error.Log(e, "用户ID获取错误", "FGBRoleSystems", UserName, "常客系统");
                    Logout();
                    return -1;
                }
            }
        }
        #endregion

        #region 获取当前集团ID
        /// <summary>
        /// 获取当前集团ID
        /// </summary>
        public static int GID
        {
            get
            {
                try
                {
                    string gid = GetCookie("gid");
                    if (gid == "")
                    {
                        return -1;
                    }
                    else
                    {
                        return int.Parse(gid);
                    }
                }
                catch (Exception e)
                {
                    HCRS.Implementation.Error.Log(e, "集团ID获取错误", "FGBRoleSystems", UserName, "常客系统");
                    Logout();
                    return -1;
                }
            }
        }
        #endregion

        #region 获取当前用户名
        /// <summary>
        /// 获取当前用户名
        /// </summary>
        public static string UserName
        {
            get
            {
                return GetUserName(UserID);
            }
        }

        public static string GetUserName(int uid)
        {
            return HCRS.Implementation.CommonDataAccess.GetString("select username from hfg_user_username where uid=" + uid);
        }
        #endregion

        #region  URL链接集团ID参数
        /// <summary>
        /// URL链接集团ID参数
        /// </summary>
        public static int GroupID
        {
            get
            {
                try
                {
                    string gid = HttpContext.Current.Request.Params.Get(FGBCommon.GetConfiguration("refgid"));
                    return Convert.ToInt32(gid);
                }
                catch
                {

                    return 0;
                }
            }
        }
        #endregion

        #region URL链接酒店ID参数
        /// <summary>
        /// URL链接酒店ID参数
        /// </summary>
        public static int HotelID
        {
            get
            {
                try
                {
                    string hid = HttpContext.Current.Request.Params.Get(FGBCommon.GetConfiguration("refhid"));
                    return Convert.ToInt32(hid);
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion

        #region 带ID的链接参数
        /// <summary>
        /// 带ID的链接参数(wgid=##&amp;whid=##)
        /// </summary>
        public static string IDUrlParams
        {
            get
            {
                int gid = GroupID;
                int hid = HotelID;

                if (gid > 0 && hid > 0)
                {
                    return FGBCommon.GetConfiguration("refgid") + "=" + gid + "&" + FGBCommon.GetConfiguration("refhid") + "=" + hid;
                }
                else if (gid > 0)
                {
                    return FGBCommon.GetConfiguration("refgid") + "=" + gid;
                }
                else if (hid > 0)
                {
                    return FGBCommon.GetConfiguration("refhid") + "=" + hid;
                }
                else
                {
                    return "";
                }
            }

        }
        #endregion

        #region 用户上次登陆
        /// <summary>
        /// 用户上次登陆IP
        /// </summary>
        public static string LogIP
        {
            get
            {
                return GetCookie("logip");
            }
        }
        #endregion

        #region 用户已登陆次数
        /// <summary>
        /// 用户已登陆次数
        /// </summary>
        public static string Logcount
        {
            get { return GetCookie("logcount"); }
        }
        #endregion

        #region 用户上次登陆时间
        /// <summary>
        /// 用户上次登陆时间
        /// </summary>
        public static string LogDate
        {
            get { return GetCookie("logdate"); }
        }
        #endregion

        #region 用户前次登陆位置
        /// <summary>
        /// 用户前次登陆位置
        /// </summary>
        public static string LogAddress
        {
            get { return HCRS.Implementation.QQIPAddress.GetAddress(GetCookie("logip")).Address; }
        }


        #endregion

        #region 获取当前用户创建时间
        /// <summary>
        /// 获取当前用户创建时间
        /// </summary>
        public static DateTime CreationDate
        {
            get
            {
                return DateTime.Now;//GetCookie("regdate");
            }
        }
        #endregion

        #region 获取当前用户最后活动时间
        /// <summary>
        /// 获取当前用户最后活动时间
        /// </summary>
        public static DateTime LastActivityDate
        {
            get
            {
                return System.DateTime.UtcNow;
            }
        }
        #endregion

        #region 获取当前用户安全问题
        /// <summary>
        /// 获取当前用户安全问题
        /// </summary>
        public static string PasswordQuestion
        {
            get
            {
                return null;
            }
        }
        #endregion

        #region  用户注册
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="gid">集团ID</param>
        /// <param name="username">用户名</param>
        /// <param name="usrpassword">用户密码</param>
        /// <param name="usremail">用户邮箱</param>
        /// <param name="usrmobile">用户手机号</param>
        /// <param name="question">安全问题</param>
        /// <param name="answer">安全问题答案</param>
        /// <param name="ademail">是否接受邮件促销信息</param>
        /// <param name="admobile">是否接受手机短信促销信息</param>
        /// <param name="msg">输出信息</param>
        /// <returns>是否成功注册</returns>
        public static bool hfg_userInsert(string emailfrom, string emailalias,int gid, string username, string usrpassword, string usremail, string usrmobile, bool ademail, bool admobile, bool IsAjax, out string msg)
        {
            string guid = Guid.NewGuid().ToString().Replace("-", "");
            if (FGBUserSystems.hfg_userInsert(gid, username, usrpassword, usremail, usrmobile, ademail, admobile, guid, IsAjax, out msg))
            {
                sendActiveEmail( emailfrom,  emailalias,"邮箱验证", username, "e", guid, usremail, IsAjax, false);
                return true;
            }
            return false;
        }

        #endregion

        #region 发送重设密码链接
        /// <summary>
        /// 发送重设密码链接
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="username"></param>
        /// <param name="useremail"></param>
        /// <param name="usrmobile"></param>
        /// <returns></returns>
        public static bool sendPasswordEmail(string emailfrom, string emailalias, int gid, string username, string useremail, string usrmobile, out FGBUserClass user)
        {
            //查找用户信息
            user = new FGBUserClass();
            string guid = Guid.NewGuid().ToString().Replace("-", "");
            string usercheckcode = CommonWeb.ToMD5(guid);
            if (!FGBUserSystems.UserIsExist(gid, username, useremail, usrmobile, "", ValidateType.UserName, out user))
            {
                return false;
            }
            bool flag = CommonDataAccess.ExecUpdateSQL("hfg_user", "usercheckcode,Checkcodedate", "string,datetime", new object[] { usercheckcode, DateTime.Now }, "gid,uid", "int,int", new object[] { gid, user.uid });
            if (user.email.ToLower().Trim() != useremail.ToLower().Trim())
            {
                return false;
            }
            if (flag && user.usremail.Length > 5)
            {
                if (sendActiveEmail( emailfrom,  emailalias,"密码重设", username, "p", guid, user.usremail, false, false))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 发送验证码邮件
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="username">用户登录名</param>
        /// <param name="codeType">类型(m:Email验证,p:重设密码)</param>
        /// <param name="guid">GUID</param>
        /// <param name="email">用户Email</param>
        /// <param name="IsAjax">是否是Ajax调用</param>
        /// <param name="IsUrl">是否是发送链接</param>
        /// <returns>是否成功</returns>
        private static bool sendActiveEmail(string emailfrom, string emailalias, string subject, string username, string codeType, string guid, string email, bool IsAjax, bool IsUrl)
        {
            string msg = "";
            string content = "";
            if (IsUrl)
            {
                string url = IsAjax ? HttpContext.Current.Request.UrlReferrer.ToString().Split('?')[0] : HttpContext.Current.Request.Url.ToString().Split('?')[0];
                int l = url.LastIndexOf("/");
                url = url.Substring(0, l);
                url = url + "/checkcode.aspx?" + IDUrlParams + "&username=" + username + "&cd=" + codeType + guid;
                content = "<a href=" + url + ">" + url + "<a>如链接无法打开，请复制此链接粘贴到浏览器地址栏验证邮箱！";
            }
            else
            {
                if (codeType == "p")
                {
                    content = "您的密码重设校验码为:<b>" + codeType + guid + "</b><br>请访问站点后按提示操作！";
                }
                else if (codeType == "e")
                {
                    content = "您的邮箱校验码为:<b>" + codeType + guid + "</b><br>请登录系统后进行邮箱认证！";
                }
            }

            return FGBCommon.SendEMail(email,emailalias,emailfrom,  subject, content, out msg);
        }
        #endregion

        #region 重新发送邮件验证码
        /// <summary>
        /// 重新发送邮件验证码
        /// </summary>
        /// <param name="gid">集团ID</param>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public static bool sendValidateEmail(string emailfrom, string emailalias, int gid, int uid, out string email)
        {
            //查找用户信息
            string guid = Guid.NewGuid().ToString().Replace("-", "");
            string usercheckcode = CommonWeb.ToMD5(guid);
             email = Email;
            if (email.Length < 5)
            {
                return false;
            }
            bool flag = CommonDataAccess.ExecUpdateSQL("hfg_user", "emailcheck,usercheckcode,Checkcodedate", "bit,string,datetime", new object[] { false, usercheckcode, DateTime.Now }, "gid,uid", "int,int", new object[] { gid, uid });
            if (flag)
            {
                if (sendActiveEmail(emailfrom,emailalias,"邮箱验证码", UserName, "e", guid, email, true, false))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 发送邮箱验证码
        /// </summary>
        /// <param name="email">用户邮箱</param>
        /// <returns></returns>
        public static bool sendValidateEmail(string emailfrom, string emailalias, out string email)
        {
            return sendValidateEmail(emailfrom,emailalias,GID, UserID, out email);
        }

        #endregion

        #region 验证邮箱
        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="cd">验证码</param>
        /// <returns></returns>
        public static bool IsValidateCode(string cd)
        {

            return FGBUserSystems.IsValidateCode(GID, UserID, cd);
        }

        #endregion

        #region 获取当前用户Email
        /// <summary>
        /// 获取当前用户Email
        /// </summary>
        public static string Email
        {
            get
            {
                return HCRS.Implementation.CommonDataAccess.GetString("select email from hfg_user_email where uid=" + UserID);
            }
        }

        #endregion

        #region 修改邮箱
        /// <summary>
        ///  修改邮箱
        /// </summary>
        /// <param name="OEmail">旧邮箱</param>
        /// <param name="NEmail">新邮箱</param>
        /// <param name="msg">返回消息</param>
        /// <returns>是否成功</returns>
        public static bool UpdateEmail(string emailfrom, string emailalias, string NEmail, out string msg)
        {
            try
            {
                bool IsSave = false;
                string guid = Guid.NewGuid().ToString().Replace("-", "");
                string OEmail=Email;
                if (NEmail == OEmail)
                {
                    msg = "邮箱相同，无需修改";
                    return false;
                }
                if (OEmail.Length > 0)
                {
                    IsSave = HCRS.Implementation.CommonDataAccess.ExecUpdateSQL(
                        "hfg_user_email", "email", "varchar", new object[] { NEmail },
                         "uid,gid", "int,int", new object[] { UserID, GID });
                }
                else
                {
                    IsSave = HCRS.Implementation.CommonDataAccess.ExecInsertSQL(
                        "hfg_user_email", "uid,gid,email", "int,int,varchar", new object[] { UserID, GID, NEmail });
                }

                if (IsSave)
                {
                    //取消邮箱认证状态 
                    msg = "更新成功";
                    HCRS.Implementation.CommonDataAccess.ExecUpdateSQL(
                        "hfg_user", "email,emailcheck,usercheckcode,Checkcodedate", "string,bit,string,datetime", new object[] { NEmail, false, guid, DateTime.Now },
                         "uid,gid", "int,int", new object[] { UserID, GID });
                    sendActiveEmail( emailfrom,  emailalias,"邮箱验证", UserName, "e", guid, NEmail, true, false);
                    return true;
                }
                else
                {
                    msg = "更新失败，邮箱已使用！";
                    return false;
                }


            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }

        }
        #endregion

        #region 获取当前用户手机号
        /// <summary>
        /// 获取当前用户手机号
        /// </summary>
        public static string Mobile
        {
            get
            {
                return HCRS.Implementation.CommonDataAccess.GetString("select mobnumb from hfg_user_mobile where uid=" + UserID);
            }
        }

        #endregion

        #region 修改手机号
        /// <summary>
        ///  修改手机号
        /// </summary>
        /// <param name="OEmail">旧手机号</param>
        /// <param name="NEmail">新手机号</param>
        /// <param name="msg">返回消息</param>
        /// <returns>是否成功</returns>
        public static bool UpdateMobile(string NMobile, out string msg)
        {
            try
            {
                bool IsSave = false;
                string OMobile = Mobile;
                if (NMobile == OMobile)
                {
                    msg = "手机号相同，无需修改";
                    return false;
                }
                if (OMobile.Length > 0)
                {
                    IsSave = HCRS.Implementation.CommonDataAccess.ExecUpdateSQL(
                        "hfg_user_mobile", "mobnumb", "varchar", new object[] { NMobile },
                         "uid,gid", "int,int", new object[] { UserID, GID });

                }
                else
                {
                    IsSave = HCRS.Implementation.CommonDataAccess.ExecInsertSQL(
                        "hfg_user_mobile", "uid,gid,mobnumb", "int,int,varchar", new object[] { UserID, GID, NMobile });
                }

                if (IsSave)
                {
                    msg = "更新成功";

                    //同步主表手机号
                    HCRS.Implementation.CommonDataAccess.ExecUpdateSQL(
                     "hfg_user", "usrmobile", "varchar", new object[] { NMobile },
                      "uid,gid", "int,int", new object[] { UserID, GID });
                    return true;
                }
                else
                {
                    msg = "更新失败，手机号已使用！";
                    return false;
                }


            }
            catch (Exception e)
            {
                msg = e.Message;
                return false;
            }

        }
        #endregion

        #region 用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="gid">集团ID</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="vtype">登陆方式</param>
        /// <param name="days">登陆有效期</param>
        /// <param name="msg">输出信息</param>
        /// <returns>是否成功</returns>
        public static bool ValidateUser(int gid, string username, string password, ValidateType vtype, CookieDays days, out string msg)
        {
            FGBUserClass user = new FGBUserClass();
            if (FGBUserSystems.ValidateUser(gid, username, password, vtype, out user))
            {
                //写用户cookie
                if (!user.usrstatus)
                {
                    HttpContext.Current.Request.Cookies.Remove(CookieKey);
                    msg = "帐户已锁定，请联系网站管理员！";
                    return false;
                }
                else
                {
                    LoginCookie((int)days, user.username, user.gid, user.uid);
                    msg = "登录成功";
                }

                return true;
            }
            else
            {
                msg = "用户名不存在或用户名密码输入错误";
                return false;
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="gid">集团ID</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="vtype">登陆方式</param>
        /// <param name="days">登陆有效期</param>
        /// <param name="msg">输出信息</param>
        /// <returns>是否成功</returns>
        public static bool ValidateUser(int gid, string username, string password, ValidateType vtype)
        {
            FGBUserClass user = new FGBUserClass();
            if (FGBUserSystems.ValidateUser(gid, username, password, vtype, out user))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ValidateUser(int gid, string username, string password, ValidateType vtype, CookieDays days, out FGBUserClass user, out string msg)
        {
            if (FGBUserSystems.ValidateUser(gid, username, password, vtype, out user))
            {
                //写用户cookie
                if (user.usrstatus)
                {
                    HttpContext.Current.Request.Cookies.Remove(CookieKey);
                    msg = "帐户已锁定，请联系网站管理员！";
                    return false;
                }
                else
                {
                    LoginCookie((int)days, user.username, user.gid, user.uid);
                    msg = "登录成功";
                }

                return true;
            }
            else
            {
                msg = "用户名不存在或用户名密码输入错误";
                return false;
            }
        }
        #endregion

        #region 用户退出
        /// <summary>
        /// 用户退出
        /// </summary>
        public static void Logout()
        {
            try
            {
                HttpCookie cookie = new HttpCookie(CookieKey);
                cookie.Expires = DateTime.Now.Date.AddDays(-5d);
                HttpContext.Current.Response.Cookies.Add(cookie);

            }
            catch
            {

            }
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="username"></param>
        /// <param name="opassword"></param>
        /// <param name="npassword1"></param>
        /// <param name="npassword2"></param>
        /// <returns></returns>
        public static bool ChangePassword(string username, string opassword, string npassword)
        {
            //FGBUserClass user = new FGBUserClass();;
            //if (user == null) return false;
            //return user.ChangePassword(opassword, npassword);
            return true;

        }
        #endregion

        #region  获取用户Cookie值
        /// <summary>
        /// 获取用户Cookie值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            try
            {
                return HttpContext.Current.Request.Cookies[CookieKey][key].ToString();
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 登录cookie写入
        /// <summary>
        /// 登录cookie写入
        /// </summary>
        /// <param name="days"></param>
        /// <param name="username"></param>
        /// <param name="gid"></param>
        public static void LoginCookie(int days, string username, int gid, int uid)
        {
            //写入用户名 cookies

            HttpCookie cookie = new HttpCookie(CookieKey);//
            cookie.Values.Add("username", username);
            cookie.Values.Add("gid", gid.ToString());
            cookie.Values.Add("uid", uid.ToString());
            cookie.Values.Add("userencrypt", HttpContext.Current.Server.UrlEncode(FGBCommon.GetaddMD5(username + (uid + gid))));//
            cookie.Values.Add("Session", HttpContext.Current.Session.SessionID);//用户是否校验
            cookie.Values.Add("days", days.ToString());//保存的时长
            cookie.Values.Add("udate", DateTime.Today.ToString("yyyy-MM-dd"));//是否已减日期

            LoginLog log = GetIPLog(uid);
            cookie.Values.Add("logcount", log.logcount.ToString());
            cookie.Values.Add("logip", log.logip);
            cookie.Values.Add("logdate", log.logdate.ToString());
            if (days < 1)
            {
                cookie.Expires = DateTime.Now.AddMinutes(20);
            }
            else
            {
                cookie.Expires = DateTime.Now.AddDays(days);
            }
            HttpContext.Current.Response.AppendCookie(cookie);
        }



        #endregion

        #region 判断是否已登陆
        /// <summary>
        /// 判断是否已登陆
        /// </summary>
        /// <returns></returns>
        public static bool IsLogin
        {
            get
            {
                try
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieKey];

                    string username = cookie["username"].ToString();
                    int gid = Convert.ToInt32(cookie["gid"]);
                    int uid = Convert.ToInt32(cookie["uid"]);
                    string userEncrypt = cookie["userencrypt"].ToString();
                    userEncrypt = HttpContext.Current.Server.UrlDecode(userEncrypt);
                    if (FGBCommon.GetaddMD5(username + (uid + gid)) == userEncrypt)
                    {
                        if (!(cookie["Session"].ToString() == HttpContext.Current.Session.SessionID.ToString()))
                        {
                            if (GroupID.ToString()!= gid.ToString())
                            {
                                Logout();
                                return false;
                            }
                            //校验用户是否冻结
                            if (!FGBUserSystems.ValidateUser(gid, uid))
                            {
                                Logout();
                                return false;
                            }

                            cookie["Session"] = HttpContext.Current.Session.SessionID.ToString();
                            LoginLog log = GetIPLog(uid);
                            cookie["logcount"] = log.logcount.ToString();
                            cookie["logip"] = log.logip.ToString();
                            cookie["logdate"] = log.logdate.ToString();
                            //用户cookie有效期减1天
                            if (cookie["days"].ToString() != "0" && !(cookie["udate"].ToString() == DateTime.Today.ToString("yyyy-MM-dd")))
                            {
                                cookie["udate"] = DateTime.Today.ToString("yyyy-MM-dd");
                                cookie.Expires = cookie.Expires.AddDays(-1d);
                            }
                            HttpContext.Current.Response.AppendCookie(cookie);
                        }
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    // HttpContext.Current.Response.Cookies.Remove(CookieKey);
                    return false;
                }
            }
        }

        #endregion

        #region 根据查询用户信息
        /// <summary>
        ///  查询用户信息
        /// </summary>
        /// <param name="gid">集团ID</param>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>

        public static FGBUserClass GetUserInfo(int gid, int uid)
        {
            return FGBUserSystems.GetUserInfo(gid, uid);
        }
        /// <summary>
        /// 登陆用户信息
        /// </summary>
        /// <returns></returns>
        public static FGBUserClass GetUserInfo()
        {
            return FGBUserSystems.GetUserInfo(GID, UserID);
        }


        #endregion

        #region 获取用户登录信息
        /// <summary>
        /// 获取用户登录信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static LoginLog GetIPLog(int uid)
        {
            string IP = HttpContext.Current.Request.UserHostAddress;
            return FGBUserSystems.GetIPLog(uid, IP);

        }
        #endregion

        #region 用户基本信息
        /// <summary>
        /// 用户基本信息
        /// </summary>
        /// <param name="username">用户名</param>

        public static void SetUserCookie(int gid, int uid)
        {
            FGBUserClass user = GetUserInfo(gid, uid);
            HttpCookie cookie = new HttpCookie(InfoKey);
            cookie["uid"] = user.uid.ToString();
            cookie["gid"] = user.gid.ToString();
            cookie["unumber"] = user.unumber.ToString();
            cookie["lvlid"] = user.lvlid.ToString();
            //cookie["endyear"]=       user.endyear.ToString();      
            //cookie["scores"]=        user.scores.ToString();       
            cookie["username"] = user.username.ToString();
            //cookie["usrpassword"]=   user.usrpassword.ToString();  
            cookie["usremail"] = user.usremail.ToString();
            //cookie["emailformat"]=   user.emailformat.ToString();  
            //cookie["emaillanguage"]= user.emaillanguage.ToString();
            //cookie["usrmobile"]=     user.usrmobile.ToString();    
            //cookie["question"]=      user.question.ToString();     
            //cookie["answer"]=        user.answer.ToString();       
            //cookie["usrfirstname"]=  user.usrfirstname.ToString(); 
            //cookie["usrlastname"]=   user.usrlastname.ToString();  
            //cookie["usrgender"]=     user.usrgender.ToString();    
            //cookie["usrbirthday"]=   user.usrbirthday.ToString();  
            //cookie["usrnationality"]=user.usrnationality.ToString()
            //cookie["idtype"]=        user.idtype.ToString();       
            //cookie["idno"]=          user.idno.ToString();         
            //cookie["usrcountry"]=    user.usrcountry.ToString();   
            //cookie["usrprovince"]=   user.usrprovince.ToString();  
            //cookie["usrcity"]=       user.usrcity.ToString();      
            //cookie["smoke"]=         user.smoke.ToString();        
            //cookie["bed"]=           user.bed.ToString();          
            //cookie["pillow"]=        user.pillow.ToString();       
            //cookie["regdate"]=       user.regdate.ToString(); 
            //----------------记录前一次登陆-------------------------------------------
            cookie["logdate"] = user.logdate.ToString();
            cookie["logip"] = user.logip.ToString();
            //----------------当前登录-------------------------------------
            cookie["logdate"] = user.logdate.ToString();
            cookie["logip"] = user.logip.ToString();
            cookie["logcount"] = user.logcount.ToString();
            //cookie["usrstatus"]=     user.usrstatus.ToString();    
            cookie["emailcheck"] = user.emailcheck.ToString().ToLower();
            //cookie["usercheckcode"]= user.usercheckcode.ToString();
            //cookie["checkcodedate"]= user.checkcodedate.ToString();
            //cookie["ademail"]=       user.ademail.ToString();      
            //cookie["admobile"]=      user.admobile.ToString();     
            //cookie["checkcodedate"]= user.checkcodedate.ToString();


            HttpContext.Current.Response.AppendCookie(cookie);


        }
        #endregion

    }
}
